//
//  HNSH_ChangePwdViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/22.
//

#import "HNSH_ChangePwdViewController.h"

//view
#import "HNSH_SafeItemView.h"

@interface HNSH_ChangePwdViewController ()

/* 密码 */
@property (nonatomic, strong) HNSH_SafeItemView *pwdView;
/* 密码 */
@property (nonatomic, copy) NSString *pwdString;
/* 确认密码 */
@property (nonatomic, strong) HNSH_SafeItemView *verifView;
/* 确认密码 */
@property (nonatomic, copy) NSString *verifString;

@end

@implementation HNSH_ChangePwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.pwdString = @"";
    self.verifString = @"";
    
    [self showNavTitle:@"修改密码" showLeftItem:YES];
    self.view.backgroundColor = [UIColor appWhiteLabelColor];
    [self configUI];
}

- (void)configUI {
    //手机号
    self.pwdView = [[HNSH_SafeItemView alloc]initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, 50) type:0];
    self.pwdView.titleString = @"输入新密码：";
    self.pwdView.placeholder = @"请输入新密码";
    [self.view addSubview:self.pwdView];
    self.pwdView.textField.secureTextEntry = YES;
    
    //验证码
    self.verifView = [[HNSH_SafeItemView alloc]initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight+50, kScreenWidth, 50) type:0];
    self.verifView.titleString = @"确认新密码：";
    self.verifView.placeholder = @"请输入确认新密码";
    [self.view addSubview:self.verifView];
    self.verifView.textField.secureTextEntry = YES;
    
    UILabel *desLabel = [[UILabel alloc]init];
    desLabel.text = @"密码必须是6-18位的英文字母、数字、符号的组合(除空格)，且字母、数字和标点符号至少包含两种";
    desLabel.textColor = [UIColor colorWithHexString:@"#8E8E93"];
    desLabel.font = [UIFont systemFontOfSize:10];
    desLabel.numberOfLines = 0;
    [self.view addSubview:desLabel];
    
    [desLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(28);
        make.right.mas_equalTo(-28);
        make.top.equalTo(self.verifView.mas_bottom).offset(14);
    }];
    
    UIButton *showButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [showButton setTitle:@"显示密码" forState:UIControlStateNormal];
    [showButton setTitleColor:[UIColor appBlackLabelColor] forState:UIControlStateNormal];
    showButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [showButton setImage:[UIImage imageNamed:@"base_select_on"] forState:UIControlStateNormal];
    [showButton setImage:[UIImage imageNamed:@"base_select_off"] forState:UIControlStateSelected];
    showButton.selected = YES;
    [showButton HNSH_layoutButtonWithEdgeInsetsStyle:HHZXButtonStyleTextRight imageTitleSpace:8];
    [self.view addSubview:showButton];
    [showButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(desLabel.mas_left);
        make.top.equalTo(desLabel.mas_bottom).offset(12);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(20);
    }];
    __weak __typeof(&*self)weakSelf = self;
    [[showButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UIButton *button = x;
        x.selected = !x.selected;
        weakSelf.pwdView.textField.secureTextEntry = x.selected;
        weakSelf.verifView.textField.secureTextEntry = x.selected;
    }];
    
    //下一步
    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    nextButton.frame = CGRectMake(30, self.verifView.origin.y+self.verifView.height+140, kScreenWidth-60, 50);
    [nextButton setTitle:@"修改密码" forState:UIControlStateNormal];
    [nextButton setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
    nextButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    nextButton.backgroundColor = [UIColor appGreenColor];
    nextButton.layer.cornerRadius = 25.0f;
    nextButton.layer.masksToBounds = YES;
    [self.view addSubview:nextButton];
    
    self.pwdView.fieldBlock = ^(NSString * _Nonnull inputString) {
        weakSelf.pwdString = inputString;
    };
    self.verifView.fieldBlock = ^(NSString * _Nonnull inputString) {
        weakSelf.verifString = inputString;
    };
    [[nextButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf loadChangePwd];
    }];
}

- (void)loadChangePwd {
    if (![self.pwdString isNotBlank]) {
        [self.view makeToast:@"请输入密码"];
        return;
    }
    if (![self.verifString isNotBlank]) {
        [self.view makeToast:@"请输入验证密码"];;
        return;
    }
    if (![self.pwdString isEqualToString:self.verifString]) {
        [self.view makeToast:@"两次密码不一致"];
        return;
    }
    
    if ([ST_UserManager alreadyLogin]) {//登录---修改密码
        [self modifyPassword];
    } else {
        [self resetPassword];
    }
}

//MARK: 未登录时修改密码
- (void)resetPassword{
    [HNSH_UserNetServer requestUserResetPasswordWithUserName:self.mobile sms_code:self.smsCode pwd:self.pwdString success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.resultset == 0) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

//MARK: 已登录时修改密码
- (void)modifyPassword{
    [HNSH_UserNetServer requestUserModifyPasswordWithSms_code:self.smsCode pwd:self.pwdString success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [ST_UserManager logOut];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
